// 二分法最终是为了 压缩范围区间
function bsearch(A, x){
    let left = 1
    let right = A.length - 1
    let guess

    // 循环结束的条件是 left 等于 right
    while(left <= right) {
        guess = Math.floor((right + left)/2)
        // 循环不变式
        // guess 等于 left和right的中间的值
        // 1.查找范围为left的右侧，以及right的左侧
        if (A[guess] === x) return guess 
        else if (A[guess] > x) right = guess - 1
        else left = guess + 1
        
    }
    return -1 // while循环结束后就没找到
}

const arr = [3,4,5,12,21,34,56,67,78,88,90,100]
console.log(bsearch(arr,88))